#!/usr/bin/env python2.7

# load_lidar_data.py

import os, sys
#import re
from datetime import date, datetime, timedelta
from dateutil.parser import parse
#import numpy as np
#import matplotlib
#matplotlib.use('Agg')
#import matplotlib.pyplot as plt
#from mpl_toolkits.basemap import Basemap
#from matplotlib import mlab
#from netCDF4 import Dataset
from metlib import *

__all__ = ['LidarDataLoader']

class LidarDataLoader(object):
    def __init__(self, datadir, fname_pattern=r'^\d{10}.*\.nc$'):
        self.datadir = datadir
        self.filelist = list_all_file(self.datadir, fname_pattern=fname_pattern)
        self.filedict = dict()
        for fname in self.filelist:
            bn = os.path.basename(fname)
            datestr = bn[0:10]
            self.filedict[datestr] = fname

    def get_file_by_date(self, datestr):
        if isinstance(datestr, (date, datetime,)):
            datestr = datestr.strftime('%Y%m%d%H')
        return self.filedict.get(datestr, None)

    def get_files_by_range(self, beg_dt, end_dt):
        beg_dt = T(beg_dt)
        end_dt = T(end_dt)
        extend_beg_dt = T(beg_dt.strftime('%Y%m%d%H'))
        extend_end_dt = end_dt + TD('1h')
        dts = datetime_range(extend_beg_dt, extend_end_dt, '1h')
        hrstrs = [dt.strftime('%Y%m%d%H') for dt in dts]
        filenames = [self.filedict.get(hrstr, None) for hrstr in hrstrs]
        filenames = filter(lambda fn: fn is not None, filenames)
        return filenames

if __name__ == '__main__':
    pass
